/**
 * Created by Administrator on 2015/6/13.
 */

function GetBitmap(texturename:string):egret.Bitmap{
    var result:egret.Bitmap = new egret.Bitmap(RES.getRes(texturename));
    //result.touchEnabled = false;
    result.cacheAsBitmap = true;
    //result.scaleX = result.scaleY = GameConst.GlobalScale;
    return result;
}
/**
function GetUIAssetsNum(num:any, con:eui.Group, scale:number=1.0,needClean:boolean=false, needSplitor:boolean=false,appends:string[]=null,type:string="preload_json.num_",showW:boolean=false){
    //if( needClean){
    //    RemoveAllElement(con);
    //}
    var clen:number = con.numElements;
    var arr:any[] = GetNumString(num,needSplitor,showW).split("");
    var len:number = arr.length;
    var bm:egret.Texture;
    var asset:eui.Image;
    var sx:number = 0;
    var tarName:string;
    var gap:number = clen - len;
    while( gap > 0){
        asset = <eui.Image>con.removeElementAt(con.numElements-1);
        AssetsPool.getInstance().reclaim(asset,0);
        gap--;
    }

    for(var i:number=0; i<len; i++){
        if( i < clen){
            asset = <eui.Image>con.getElementAt(i);
        }else{
            asset = AssetsPool.getInstance().produce(AssetsPool.UiAsset);
        }

        if( arr[i] != ","  && arr[i] != "."  && arr[i] != "/" && arr[i] != ":" && arr[i] != "w"  && arr[i] != "%" )
        {
            tarName = type+arr[i];
        }
        else if(arr[i] == ","){
            tarName = type+"d";
        }else if(arr[i] == "."){
            tarName = type+"dot";
        }else if( arr[i] == "/"){
            tarName = type+"per";
        }else if( arr[i] == ":"){
            tarName = type+"hehe";
        }else if( arr[i] == "w"){
            tarName ="img_wan";
        }else if( arr[i] == "%"){
            tarName ="img_%";
        }
        if( asset.name != tarName){
            bm = RES.getRes(tarName);
            asset.name = tarName;
            asset.source = bm;
            asset.scaleX=asset.scaleY=scale;
            asset.cacheAsBitmap = true;
            asset.touchChildren = asset.touchEnabled = false;
        }
        asset.x = sx;
        if( i >= clen){
            con.addChild(asset);
        }
        sx += asset.content.textureWidth*scale;
    }

    if(appends!=null){
        while(appends.length != 0 ){
            bm = RES.getRes(appends.pop());
            asset = AssetsPool.getInstance().produce(AssetsPool.UiAsset);
            asset.source = bm;
            asset.x = sx;
            asset.scaleX=asset.scaleY=scale;
            asset.cacheAsBitmap = true;
            asset.touchChildren = asset.touchEnabled = false;
            con.addChild(asset);
            sx += bm.textureWidth*scale;
        }
    }
}
 */

function LimitStringLen(str:string,len:number=7):string{
    str = str.trim();
    if( str.length > len){
        return str.substr(0,len-2)+"..";
    }
    return str;
}

function GetNumString(num:number,needSplitor:boolean=false,showW:boolean=false):string{
    if(showW){
        if( num >= 100000000){
            return (Math.floor(num/10000000))/10+"e";
        }else  if( num >= 10000){
            return (Math.floor(num/1000))/10+"w";
        }else{
            return num.toString();
        }
    }else if( !needSplitor || num < 1000){
        return num.toString();
    }
    else{
        var str:string = num.toString();
        var tstr:string = "";
        var len:number = str.length;
        var idx:number = 0;
        while(len > 0){
            if( idx != 2 || len == 1){
                tstr = str.charAt(len-1)+tstr;
            }else{
                tstr = ","+str.charAt(len-1)+tstr;
                idx = -1;
            }
            idx++;
            len--;
        }
        return tstr;
    }
}

function GetNum(num:any,con:egret.DisplayObjectContainer, type:number=0,needClean:boolean=true, scale:number=1.0){
    if( needClean )
        RemoveAllChild(con);
    var arr:any[] = num.toString().split("");
    var len:number = arr.length;
    var bm:egret.Bitmap;
    var sx:number = 0;
    for(var i:number=0; i<len; i++){
        bm = AssetsPool.getInstance().produce(AssetsPool.BitMap);
        if( arr[i] != "." && arr[i] != "/")
        {
            bm.texture = RES.getRes("preload_json.number1_"+arr[i]);
        }else if(arr[i] == "."){
            bm = RES.getRes("preload_json.number1_d");
        }else if( arr[i] == "/"){
            bm = RES.getRes("preload_json.per");
        }
        bm.x = sx;
        bm.scaleX = bm.scaleY = scale;
        bm.cacheAsBitmap = true;
        bm.touchEnabled = false;
        sx += bm.width*scale;
        con.addChild(bm);
    }
}
function RemoveAllElement(container:eui.Group){
    while(container.numElements != 0){
        var element:any = container.removeChildAt(0);
        AssetsPool.getInstance().reclaim(element,0);
    }
}
function RemoveAllChild(container:egret.DisplayObjectContainer){
    while(container.numChildren != 0){
        var child:any = container.removeChildAt(0);
        AssetsPool.getInstance().reclaim(child,1);
    }
}

function ShowHint(content:string){
    if( GameConst.HintText == null){
        GameConst.HintObj = new egret.DisplayObjectContainer();
        GameConst.HintBg = new egret.Bitmap(RES.getRes("hint_bg"));
        GameConst.HintBg.scale9Grid = new egret.Rectangle(5,10,1,1);
        GameConst.HintText =  new egret.TextField();
        GameConst.HintText.textColor = 0xffe154;
        GameConst.HintText.bold = true;
        GameConst.HintText.size = 18;
        GameConst.HintText.x = 5;
        GameConst.HintText.y = 5;
        GameConst.HintText.fontFamily = "微软雅黑";
        GameConst.HintText.cacheAsBitmap = true;
        GameConst.HintObj.addChild(GameConst.HintBg);
        GameConst.HintObj.addChild(GameConst.HintText);
        GameConst.HintMoveItem = new MoveItem();
    }
    if( GameConst.ExtObj == null){
        GameConst.ExtObj = {
            name : "extObj",
            onFinish:function(dalta:number){
                if( GameConst.HintObj.parent)
                    GameConst.Stage.removeChild(GameConst.HintObj);
            }
        };
    }else{
        GameConst.ExtObj.cnt = 60;
    }
    GameConst.HintMoveItem.init(GameConst.HintObj, GameConst.HintObj.x,GameConst.HintObj.y-120,1000, GameConst.ExtObj,GameConst.ExtObj.onFinish);
    if( !GameConst.Stage.contains(GameConst.HintObj) )
        GameConst.Stage.addChild(GameConst.HintObj);
    GameConst.HintText.text =  content;
    GameConst.HintBg.width = GameConst.HintText.width+10;
    GameConst.HintObj.x = (GameConst.StageW - GameConst.HintObj.width)>> 1;
    GameConst.HintObj.y = GameConst.StageH >> 1;
    GameConst.HintObj.cacheAsBitmap = true;
    MoveMgr.getInstance().addItem(GameConst.HintMoveItem);
   //FrameMgr.getInstance().addControll(GameConst.HintFrameItem);
    //GameConst.Stage.addEventListener(egret.Event.ENTER_FRAME,GameConst.ExtObj.onHint, GameConst.ExtObj);
}

function CleanHint(){
    if( GameConst.HintMoveItem)
    {
       if( MoveMgr.getInstance().contains(GameConst.HintMoveItem)){
           GameConst.HintMoveItem.finish();
           MoveMgr.getInstance().delItem(GameConst.HintMoveItem);
       }
        if( GameConst.HintObj.parent)
            GameConst.Stage.removeChild(GameConst.HintObj);
    }
}
/**
 * time millseconds
 * type:
 * 0 only seconds
 * 1 minutes&seconds
 * 2 hour&minutes&seconds
 * **/
function GetTimeStr(time:number,type:number=2,transSimbol:boolean = false):string{
    var result:string="";
    if( type == 2){
        var hour:number = Math.floor(time/3600000);
        if( hour < 10){
            result += "0"+hour+ (!transSimbol ? ":" :"m");
        }else{
            result += hour+  (!transSimbol ? ":" :"m");
        }
    }
    time = time%3600000;
    if( type >= 1){
        var minute = Math.floor(time/60000);
        if( minute < 10){
            result += "0"+minute+  (!transSimbol ? ":" :"m");
        }else{
            result += minute+ (!transSimbol ? ":" :"m");
        }
    }

    time = time%60000;
    var second = Math.floor(time/1000);
    if( second < 10){
        result += "0"+second;
    }else{
        result += second;
    }
    return result;
}

function GetDistance(x0:number, y0:number, x1:number, y1:number):number{
    var dx:number = x0 - x1;
    var dy:number = y0 - y1;
    return Math.sqrt(dx*dx+dy*dy);
}

function QuickSort(arr:any[],prop:string,left:number,right:number){
    var i;
    var j;
    var t;
    var temp;
    if( left > right)
        return;

    temp = arr[left];
    i =  left;
    j = right;
    while( i != j){
        while(arr[j][prop] >= temp[prop] && i < j)
            j--;

        while(arr[i][prop] <= temp[prop] && i < j)
            i++;

        if( i < j){
            t = arr[i];
            arr[i] =arr[j];
            arr[j] = t;
        }
    }
    arr[left] = arr[i];
    arr[i] = temp;
    QuickSort(arr,prop,left,i-1);
    QuickSort(arr,prop, i+1, right);
}